<?php

namespace app\controller\api\store\merchant;

use app\validate\merchant\DeliveryStationValidate;
use crmeb\services\DeliverySevices;
use think\App;
use crmeb\basic\BaseController;
use app\common\repositories\delivery\DeliveryExpressStationRepository;
use app\common\repositories\delivery\DeliveryStationRepository;
use app\validate\merchant\DeliveryExpressStationValidate;
use think\exception\ValidateException;

class DeliveryStation extends BaseController
{
    protected $merId;
    protected $tipMsg;
    protected $deliveryStatus;
    protected $deliveryType;
    protected $repository;

    public function __construct(App $app, DeliveryExpressStationRepository $ExpressStationRepository, DeliveryStationRepository $StationRepository)
    {
        parent::__construct($app);
        $this->merId = $this->request->route('merId');
        $this->deliveryType = $this->request->param('delivery_type',null);
        if(!in_array($this->deliveryType,['same_city','express_delivery'])){
            throw new ValidateException('参数错误');
        }
        $this->repository = $this->deliveryType == 'same_city' ? $StationRepository : $ExpressStationRepository;
        $this->deliveryStatus =  $this->deliveryType == 'same_city' ? systemConfig('delivery_status') : systemConfig('delivery_express_status');
        $this->tipMsg =   $this->deliveryType == 'same_city' ? "未开启同城配送" : "未开启平台快递配送";
    }

    public function lst()
    {
        [$page, $limit] = $this->getPage();
        $where = $this->request->params(['keyword','station_name','status','date']);
        $where['mer_id'] = $this->merId;
        $data = $this->repository->merList($where, $page, $limit);
        return app('json')->success($data);
    }

    public function detail($id)
    {
        $data = $this->repository->detail($id,$this->merId);
        return app('json')->success($data);
    }

    public function create()
    {
        if ($this->deliveryStatus != 1) throw new ValidateException($this->tipMsg);
        $data = $this->checkParams();
        $data['mer_id'] = $this->merId;
        $this->repository->save($data);
        return app('json')->success('添加成功');
    }

    public function update($id)
    {
        if ($this->deliveryStatus != 1) throw new ValidateException($this->tipMsg);
        $data = $this->checkParams();
        $this->repository->edit($id, $this->merId, $data);
        return app('json')->success('编辑成功');
    }

    public function delete($id)
    {
        if ($this->deliveryStatus != 1) throw new ValidateException($this->tipMsg);
        $this->repository->destory($id, $this->merId);
        return app('json')->success('删除成功');
    }

    public function switchWithStatus($id)
    {
        if ($this->deliveryStatus != 1) throw new ValidateException($this->tipMsg);
        $status = $this->request->param('status') == 1 ? 1 : 0;
        $this->repository->update($id,['status' => $status]);
        return app('json')->success('修改成功');
    }

    public function getBusiness()
    {
        if ($this->deliveryStatus != 1 && $this->deliveryType != 'same_city') throw new ValidateException($this->tipMsg);
        $data = $this->repository->getBusiness();
        return app('json')->success($data);
    }


    public function getCityLst()
    {
        if ($this->deliveryStatus != 1) throw new ValidateException($this->tipMsg);
        return app('json')->success($this->repository->getCityLst());
    }

    public function mark($id)
    {
        if ($this->deliveryStatus != 1) throw new ValidateException($this->tipMsg);
        $data = $this->request->params(['mark']);
        $this->repository->update($id, $data);
        return app('json')->success('备注成功');
    }

    public function checkParams()
    {

        if($this->deliveryType == 'same_city'){
            $data = $this->request->params([
                'station_name',
                'business',
                'station_address',
                'lng',
                'lat',
                'contact_name',
                'phone',
                'username',
                'password',
                ['status',1],
                'city_name',
            ]);
            $make = app()->make(DeliveryStationValidate::class);
            $data['type'] = systemConfig('delivery_type');
            if ($data['type'] == DeliverySevices::DELIVERY_TYPE_DADA) {
                $make->scene('dada')->check($data);
            } else {
                $make->check($data);
                [$data['lng'],$data['lat']] = gcj02ToBd09($data['lng'],$data['lat']);
            }
        }else{
            $data = $this->request->params([
                'station_name',
                'station_address',
                'lng',
                'lat',
                'contact_name',
                'phone',
                'province_name',
                'area_name',
                ['status',1],
                'city_name',
            ]);
            $make = app()->make(DeliveryExpressStationValidate::class);
            $make->check($data);
        }
        return $data;
    }


}
